package com.zhuoyue.biz.core.api.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhuoyue.biz.core.api.dao.SceneControllerBindingDao;
import com.zhuoyue.biz.core.api.domain.SceneControllerBindingDO;
import com.zhuoyue.biz.core.api.service.SceneControllerBindingService;
import com.zhuoyue.common.constant.ResultCode;
import com.zhuoyue.common.dto.BaseResultDTO;
import com.zhuoyue.common.dto.BatchResultDTO;


/**
 * @author Administrator
 */
@Service("sceneControllerBindingService")
public class SceneControllerBindingServiceImpl extends ServiceImpl<SceneControllerBindingDao, SceneControllerBindingDO> implements SceneControllerBindingService {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private SceneControllerBindingDao sceneControllerBindingDao;


    @Override
    public List<SceneControllerBindingDO> getSceneControllerBindingListByDeviceId(String deviceId) {
        List<SceneControllerBindingDO> list = sceneControllerBindingDao.selectSceneControllerBindingListByDeviceId(deviceId);
        if (CollectionUtils.isEmpty(list)) {
            throw new ZyServerException(ErrorType.result_not_exist.getCode(), "该场景面板未绑定场景！");
        }
        return list;
    }


    @Override
    public SceneControllerBindingDO addSceneControllerBinding(SceneControllerBindingDO scb) {

        int count = sceneControllerBindingDao.insert(scb);
        if (count < 1) {
            throw new ZyServerException(ErrorType.OBJECT_NOT_FOUND.getCode(), "添加失败");
        }
        return sceneControllerBindingDao.selectByMyId(scb.getId());
    }

    @Override
    public BaseResultDTO deleteSceneControllerBindingById(String id) {
        BaseResultDTO result = new BaseResultDTO();

        try {
            int count = sceneControllerBindingDao.deleteSceneControllerBindingById(id);
            if (count < 1) {
                result.setSuccess(false);
                result.setResultCode(ResultCode.FAIL);
                result.setErrorDetail("删除场景面板绑定信息失败！");
                return result;
            }

            result.setSuccess(true);
            result.setResultCode(ResultCode.SUCCESS);
        } catch (Exception e) {
            log.error("SceneControllerBindingServiceImpl call deleteSceneControllerBindingById error ==== ", e);
            result.setSuccess(false);
            result.setResultCode(ResultCode.SYS_ERROR);
            result.setErrorDetail("删除场景面板绑定信息系统异常！");
        }
        return result;
    }


}
